package com.example.demo.service;

import com.example.demo.vo.Boutlistdetail;
import lombok.Data;
import org.apache.commons.lang3.tuple.Triple;
import org.springframework.data.util.Pair;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * Created by ltian on 2020/1/17.
 */
@Service
public class DetailService {


    // 返回
    // 旧的包含而新的不包含的
    // 新的包含而旧的不包含的
    // 新旧都包含, 非ke对应旧值
    public Triple<List<Boutlistdetail>, List<Boutlistdetail>, List<Boutlistdetail>> compare(List<Boutlistdetail> oldDetails, List<Boutlistdetail> newDetails) {
        Map<DetailKey, Boutlistdetail> oldMap = oldDetails.stream().collect(Collectors.toMap(this::toKey, r -> r));
        Map<DetailKey, Boutlistdetail> newMap = newDetails.stream().collect(Collectors.toMap(this::toKey, r -> r));

        List<Boutlistdetail> diffOld = new ArrayList<>();
        List<Boutlistdetail> diffNew = new ArrayList<>();
        List<Boutlistdetail> same = new ArrayList<>();
        oldMap.forEach((k, v) -> {
            if (!newMap.containsKey(k)) {
                diffOld.add(v);
            } else {
                same.add(v);
            }
        });

        newMap.forEach((k, v) -> {
            if (!oldMap.containsKey(k)) {
                diffNew.add(v);
            }
        });

        return Triple.of(diffOld, diffNew, same);

    }


    private DetailKey toKey(Boutlistdetail detail) {
        DetailKey detailKey = new DetailKey();
        detailKey.setProvinceid(detail.getProvinceid());
        detailKey.setTollintervalscount(detail.getTollintervalscount());
        detailKey.setTollintervalsgroup(detail.getTollintervalsgroup());
        return detailKey;
    }


    // 认为以下字段相同的明细可以认为是业务上相同的
    @Data
    private static class DetailKey {
        private String provinceid;
        private Long tollintervalscount;
        private String tollintervalsgroup;
    }


}
